/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.conditions 
{
	import net.babeuf.form.fields.AbstractField;
	import net.babeuf.form.utils.DateUtils;
	
	/**
	 * ValidAgeCondition condition is used to check that a given age is inside a range condition 
	 * 
	 * @example
	 * <listing>
	 * var day : DayField = new DayField( tfDay ) ; 
	 * var month: MonthField = new MonthField( tfMonth ) ; 
	 * var year : YearField = new YearField( tfYear ) ; 
	 * 
	 * var condition : ValidAgeCondition = new ValidAgeCondition( day, month, year, 18, 77 ) ; 
	 * 
	 * trace(condition.match()) ; // outputs true if the current age is between 18 and 77 yo
	 * 
	 * </listing> 
	 * 
	 * @author damien pegoraro 
	 * 
	 * @see net.babeuf.form.fields.input.DayField
	 * @see net.babeuf.form.fields.input.MonthField
	 * @see net.babeuf.form.fields.input.YearField
	 * 
	 * @langversion 3.0
	 * @playerversion Flash 10
	 */
	public class ValidAgeCondition extends ValidDateCondition 
	{
		/**
		 * The minimum age to match
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		protected var _min : uint ; 
		
		/**
		 * The maximum age to match
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		protected var _max : uint ; 
		
		/**
		 * Creates a new instance
		 * 
		 * @param	day the input day field
		 * @param	month the input month field
		 * @param	year the input year field
		 * @param	min the minimum age to match
		 * @param	max the maximum age to match
		 * 
		 * @langversion 3.0
		 * @playerversion Flash 10
		 */
		public function ValidAgeCondition( day : AbstractField, month : AbstractField, year : AbstractField, min : uint = 0, max : uint = 1000 ) 
		{
			super(day, month, year) ; 
			_min = min ; 
			_max = max ; 
		}
		
		/**
		 * @inheritDoc
		 */
		override public function match():Boolean 
		{
			var s:  String = DateUtils.formatYYYYMMDD(_day.getValue(), _month.getValue(), _year.getValue() ) ;	
			if ( !DateUtils.isValidDate(s) || !DateUtils.isOlderThan( s, _min ) || !DateUtils.isYoungerThan( s, _max ) ) { _updateSensors( false ) ;  return false ;  }			
			return   true  ; 
		}
		
		
	}

}